package com.ac.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.base.helper.Result;
import com.ac.utils.XssUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;

@Component
@Aspect
@Order(2)
public class XssParamAspect {

    private Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

    private ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Pointcut("execution(public * com.ac.api.*.controller.*.*(..))")
    public void xssPoint() {
    }
    @Before("xssPoint()")
    public void paramValid(JoinPoint point) throws Throwable {
        logger.info("--------进入xss切点-------------");
        Object[] args = point.getArgs();
        for (Object o : args) {
            if (o instanceof HttpServletResponse || o instanceof HttpServletRequest) {
                // 如果参数类型是 HttpServletResponse 或 HttpServletRequest，则跳过
                continue;
            } else if (o instanceof Number) {
                continue;
            } else if (o instanceof String) {
                XssUtils.stripXSS(o.toString());
            } else if (o instanceof BindingResult || o instanceof MultipartFile || o instanceof ArrayList || o == null) {
                // 如果参数类型是 BindingResult、MultipartFile、ArrayList 或 null，则跳过
                continue;
            } else {
                Class clazz = o.getClass();
                String paramStr = JSON.toJSONString(o);
                // 使用fastjson将请求的参数转换为map
                HashMap<String, Object> map = JSONObject.parseObject(paramStr, new TypeReference<HashMap<String, Object>>() {});
                map.forEach((k, v) -> {
                    if (!"token".equals(k) && v instanceof String) {
                        map.put(k, XssUtils.stripXSS(k, v.toString()));
                    }
                });
                // 将map转为json
                String json = JSONObject.toJSONString(map);
                // 将JSON字符串转换为对象
                Object o1 = JSON.parseObject(json, clazz);
                BeanUtils.copyProperties(o1, o);
            }
        }
    }

    @AfterReturning(returning = "result", pointcut = "xssPoint()")
    public void doAfterReturning(JoinPoint joinPoint, Result result) throws IllegalAccessException {
        // 在返回请求之前对返回的内容进行过滤转义
        logger.info("过滤之前 : " + result.toString());
        XssUtils.encodeHtml(result);
//        BeanUtils.copyProperties(result1, result);
    }
}

